﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;
namespace WebApplication1
{
    public partial class WebForm2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        //注册添加
        protected void button_Click(object sender, EventArgs e)
        {
            string name = textname.Text.Trim();//trim去除空格
            int pwd = Convert.ToInt32(textpwd.Text.Trim());
            string tel = texttel.Text.Trim();
            int sex = Convert.ToInt32(textsex.SelectedValue);
            int type = Convert.ToInt32(texttype.SelectedValue);
            string time = DateTime.Now.ToString();
            string img_url = Image1.ImageUrl;
            //连接数据库
            MySqlConnection connection = new MySqlConnection("server=localhost;uid = root;database=data;charset=utf8");
            string sql = string.Format("insert into student(name,pwd,sex,tel,type,time,img_url) values('{0}',{1},'{2}','{3}',{4},'{5}','{6}')", name, pwd, sex, tel, type, time,img_url);
            MySqlCommand cmd = new MySqlCommand(sql, connection);

            //打开数据库
            connection.Open();
            int flag = cmd.ExecuteNonQuery();//执行并返回受影响行数
            connection.Close();
            if (flag == 1)
            {
                Response.Write("<script>alert('注册成功');window.location.href='WebForm1.aspx'</script>");
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string username = ".jpg.png.gif";
            string name = FileUpload1.FileName;
            string exname = name.Substring(name.LastIndexOf('.'));
            if (username.Contains(exname) == false)
            {
                Response.Write("<script>alert('不支持文件格式！')</script>");
                return;
            }
            if (FileUpload1.FileContent.Length > 1 * 1024 * 1024)
            {
                Response.Write("<script>alert('不能上传超过1M的图片！')</script>");
            }
            //1.获取文件名称
            //2.设置文件保存路径
            string path = Server.MapPath("img") + "\\" + name;
            //3.调用空间执行文件保存
            if (name != "")
            {
                FileUpload1.SaveAs(path);
            }
            //4.给Image组件夫路径，让他显示图片
            Image1.ImageUrl = "img/" + name;//虚拟地址
        }
    }
}